﻿using de.wolfmarco.pas.Client.Properties;
using de.wolfmarco.pas.Shared.Helper;
using de.wolfmarco.pas.Shared.Interfaces;
using de.wolfmarco.pas.Shared.Language;
using de.wolfmarco.pas.Shared.MessageBoxes;
using de.wolfmarco.pas.Shared.MVVM;
using de.wolfmarco.pas.Shared.Resources;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace de.wolfmarco.pas.Client.MVVM
{
    public class StartMenuModuleViewModel : ModuleViewModel
    {
        private const int BUTTON_WIDTH = 128;
        private const int BUTTON_HEIGHT = 128;
        private const int BORDER_THICKNESS = 2;
        private const int FONT_SIZE = 14;
        private const int LABEL_HEIGHT = FONT_SIZE * 3;

        public StartMenuModuleViewModel( IRegionManager regionManager,IModuleManager moduleManager )
            : base( regionManager, moduleManager )
        {
            ChangeViewModeCommand = new DelegateCommand<ViewMode?>(OnTryChangeViewMode, CanChangeViewMode);
        }

        #region ChangeViewModeCommand

        public DelegateCommand<ViewMode?> ChangeViewModeCommand { get; private set; }

        private void OnTryChangeViewMode(ViewMode? viewMode)
        {
            try
            {
                OnChangeViewMode(viewMode);
            }
            catch (Exception ex)
            {
                PASMessageBox.ShowException(ex);
            }
        }

        protected virtual bool CanChangeViewMode(ViewMode? viewMode)
        {
            return true;
        }

        protected virtual void OnChangeViewMode(ViewMode? viewMode)
        {
            if (viewMode.HasValue && viewMode.Value != Settings.Default.StartMenuViewMode)
            {
                Settings.Default.StartMenuViewMode = viewMode.Value;
                Settings.Default.Save();
                CreateModuleButtons();
            }
        }

        #endregion ChangeViewModeCommand

        #region ModuleButtons

        private IModuleCatalog moduleCatalog = null;
        private Panel panel = null;

        public void CreateModuleButtons(IModuleCatalog moduleCatalog, Panel panel)
        {
            this.moduleCatalog = moduleCatalog;
            this.panel = panel;

            CreateModuleButtons();
        }

        public void CreateModuleButtons()
        {
            if (moduleCatalog == null || panel == null)
                return;

            panel.Children.Clear();

            foreach (ModuleInfo moduleInfo in moduleCatalog.Modules)
            {
                if (ClientHelper.IsExternalModule(moduleInfo))
                {
                    if (moduleInfo.InitializationMode != InitializationMode.OnDemand)
                        throw new InvalidOperationException(
                            "InitializationMode of module '" + moduleInfo.ModuleName + "' is not OnDemand");

                    panel.Children.Add(createModuleButton(moduleInfo));
                }
            }
        }

        private Button createModuleButton( ModuleInfo moduleInfo )
        {
            StackPanel stackpanel = new StackPanel();

            switch( Settings.Default.StartMenuViewMode )
            {
                case ViewMode.ImageAndText:
                    stackpanel.Children.Add( Images.GetModuleImage( moduleInfo.ModuleName, typeof(Image128Enum) ) );
                    stackpanel.Children.Add( moduleTextBlock( moduleInfo ) );
                    break;
                case ViewMode.Image:
                    stackpanel.Children.Add( Images.GetModuleImage( moduleInfo.ModuleName, typeof(Image128Enum) ) );
                    break;
                case ViewMode.Text:
                    stackpanel.Children.Add( moduleTextBlock( moduleInfo ) );
                    break;
            }

            Button button = new Button();
            button.FontSize = FONT_SIZE;
            button.Width = BUTTON_WIDTH;
            button.Content = stackpanel;
            button.BorderThickness = new Thickness( BORDER_THICKNESS );

            button.ContextMenu = createContextMenu();

            BindingHelper.BindElementToModule(button, moduleInfo);

            return button;
        }

        private TextBlock moduleTextBlock(ModuleInfo moduleInfo)
        {
            TextBlock textBlock = new TextBlock();
            textBlock.Text = ModuleDisplayNames.GetModuleDisplayName(moduleInfo);
            textBlock.TextAlignment = TextAlignment.Center;
            textBlock.TextWrapping = TextWrapping.Wrap;
            return textBlock;
        }

        #endregion ModuleButtons

        #region ContextMenu

        private ContextMenu createContextMenu()
        {
            ContextMenu menu = new ContextMenu();

            MenuItem imageAndTextItem = new MenuItem();
            imageAndTextItem.Header = "ImageAndText";
            BindingHelper.BindElement(imageAndTextItem, "ChangeViewModeCommand", ViewMode.ImageAndText);

            MenuItem imageItem = new MenuItem();
            imageItem.Header = "Image";
            BindingHelper.BindElement(imageItem, "ChangeViewModeCommand", ViewMode.Image);

            MenuItem textItem = new MenuItem();
            textItem.Header = "Text";
            BindingHelper.BindElement(textItem, "ChangeViewModeCommand", ViewMode.Text);

            menu.Items.Add(imageAndTextItem);
            menu.Items.Add(imageItem);
            menu.Items.Add(textItem);

            return menu;
        }

        #endregion ContextMenu
    }

    public enum ViewMode
    {
        ImageAndText,
        Image,
        Text
    }
}
